<?php

namespace Respect\Http\Module;

use Respect\Http\Bootstrappable;
use Respect\Environment\EnvironmentableInterface;

abstract class Auth implements Bootstrappable
{

    public $type = '';

    public function __construct(EnvironmentableInterface $env)
    {
        $authHeader = $this->stripAuthType($env['HTTP_AUTHORIZATION']);
        $this->parseHeader($authHeader);
    }

    public static function qualify(EnvironmentableInterface $env)
    {
        $h = $env['HTTP_AUTHORIZATION'];
        if (!$h)
            return false;
        $type = static ::guessType($h);
        return new $type($http);
    }

    public static function guessType($authorizationHeader)
    {
        $typeParts = explode(' ', trim($authorizationHeader));
        return __CLASS__ . '\\' . array_shift($typeParts);
    }

    protected function stripAuthType($authHeader)
    {
        return substr($authHeader, strlen($this->type) + 1);
    }

}
